package com.sys.controller;

import com.sys.entity.SysMenu;
import com.sys.service.SysMenuService;
import com.sys.service.SysRoleMenuService;
import com.bhudy.base.QueryFilter;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.bhudy.utils.ResponseResult;
import com.sys.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author bhudy
 * @Description 菜单controller
 * @Date 2019-10-30 23:09:37
 */
@Controller
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    //  @PathVariable("field")

    // 登陆界面跳转
    @GetMapping(value = "/sysLogin")
    public String loginPage() {
        return "/sys/sysLogin";
    }

    //退出登录
    @GetMapping("/sysLogout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return "/sys/sysLogin";
    }

    @RequestMapping({"/", "/index", "/sys/index"})
    public String index() {
        return "/sys/sysIndex";
    }

    /**
     * 页面跳转
     *
     * @param url     请求的url
     * @param request HttpServletRequest
     * @return 页面路径
     */
    @GetMapping(value = "/sysRedirect")
    public String redirect(String url, HttpServletRequest request) {
        if (url == null || url.equals("")) throw new SysException(SysExceptionCode.CODE_40003);
        /*SysMenu sysMenu = new SysMenu();
        sysMenu.setPath(url);*/
        QueryFilter queryFilter = new QueryFilter().and("url", url);
        SysMenu sysMenu = sysMenuService.find(queryFilter);
        if (sysMenu == null || sysMenu.getPath() == null || sysMenu.getPath().equals(""))
            throw new SysException(SysExceptionCode.CODE_40014);

        String method = request.getMethod().toLowerCase();
        // String path = sysMenu.getPath();
        Subject subject = ThreadContext.getSubject();//创建subject实例
        if (subject.isPermitted(method + ":" + url)) {
            return sysMenu.getPath();
        } else {
            throw new SysException(SysExceptionCode.CODE_40013);
        }
    }

    /**
     * 普通菜单列表
     *
     * @param name        名称
     * @param type        类型
     * @param isLoginUser 是否查询当前登录用户的数据
     * @param page        页数
     * @param limit       显示数量
     * @return sysMenu菜单列表
     */
    @ResponseBody
    @GetMapping(value = "/api/v1/sysMenus")
    public Object getSysMenus(String name, Integer type, Boolean isLoginUser, Integer page, Integer limit) {
        QueryFilter queryFilter = new QueryFilter();
        if (isLoginUser == null || !isLoginUser) {
            if (name != null && name.length() != 0) {
                queryFilter.andLike("name", "%" + name + "%");
            }
            if (type != null) {
                queryFilter.and("type", type);
            }
            if (page != null && limit != null) queryFilter.limit(--page, limit);
            queryFilter.orderASC("level", "menuOrder");
        } else {
            queryFilter.sql("where id in (select menu_id from sys_role_menu where role_id in (select role_id from sys_user_role where user_id = ?)) and type = 1 order by menu_order", ShiroUtils.getUserId());
        }
        return ResponseResult.printMapJson(0, sysMenuService.count(queryFilter), sysMenuService.findList(queryFilter));
    }

    @ResponseBody
    @GetMapping(value = "/api/v1/sysMenuJson")
    public Object getSysMenuJson() {
        return ResponseResult.printMapJson(0, sysMenuService.findMapList("select url, name from sys_menu where status = 0"));
    }

    /**
     * 树形菜单列表
     *
     * @return sysMenu的树形菜单列表
     */
    @ResponseBody
    @GetMapping(value = "/api/v1/sysTreeMenus")
    public Object getSysTreeMenus() {
        return ResponseResult.printMapJson(0, sysRoleMenuService.getTree(null, 1));
    }


}
